package com.homework.demo3;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/3/21 19:58
 * @description
 */
public class StudentTest {

    public static void main(String[] args) {
        //在控制台输入格式为"张三/18/男/99.5"的学生若干，存于List集合中。
        Scanner input = new Scanner(System.in);
        ArrayList<Student> students = new ArrayList<>();
        for (int i = 0; i < 5; i++) {//存入5个学生 多了不想写
            System.out.print("请输入学生姓名：");
            String name = input.next();
            System.out.print("请输入学生年龄：");
            int age = input.nextInt();
            System.out.print("请输入学生性别：");
            String sex = input.next();
            System.out.print("请输入学生成绩：");
            double score = input.nextDouble();
            Student student = new Student(name, age, sex, score);
            students.add(student);
            System.out.println("------------------");
        }


        //①从数组遍历所有内容解析为学生对象，将学生在存于一个新的List集合
        ArrayList<Student> students1 = new ArrayList<>();
        for (Student student : students) {
            students1.add(student);
        }
        for (Student student : students1) {
            System.out.println(student);
        }
        System.out.println("-------------------------------");
        //②遍历集合找出"优秀"的学生信息（优秀：成绩>=80）
        System.out.println("成绩>=80的有：");
        for (Student student : students) {
            if (student.getScore()>=80){
                System.out.println(student);
            }
        }
        System.out.println("-------------------------------");
        //③找出集合中没有参加考试的学生信息（成绩为null）
        System.out.println("没有参加考试的学生:");
        boolean flag = true;
        for (Student student : students) {
            if (student.getScore()==0){
                System.out.println(student);
                flag=false;
            }
        }
        if (flag){
            System.out.println("都参加考试了");
        }

        System.out.println("-------------------------------");
        //④排除集合中的重复元素
        System.out.println("排除集合中的重复元素");
        /*for (int i = 0; i < students.size(); i++) {
            if (i==students.size()-1){
                break;
            }
            String name = students.get(i).getName();
            String name1 = students.get(i+1).getName();

            int age = students.get(i).getAge();
            int age1 = students.get(i+1).getAge();

            String sex = students.get(i).getSex();
            String sex1 = students.get(i+1).getSex();

            double score = students.get(i).getScore();
            double score1 = students.get(i+1).getScore();

            if (name.equals(name1)&&
            age==age1&&sex.equals(sex1)&&score==score1){
                students.remove(i+1);
                i--;
            }
        }*/
        for (int i = 0; i < students.size(); i++) {

            for (int j =i+1; j < students.size(); j++) {
                if (i==students.size()-1){
                    break;
                }

                String name = students.get(i).getName();
                int age = students.get(i).getAge();
                String sex = students.get(i).getSex();
                double score = students.get(i).getScore();

                String name1 = students.get(j).getName();
                int age1 = students.get(j).getAge();
                String sex1 = students.get(j).getSex();
                double score1 = students.get(j).getScore();

                if (name.equals(name1)&&age==age1&&sex.equals(sex1)&&score==score1){
                    System.out.println(students.get(j));
                    students.remove(j);
                    i++;
                    break;
                }

            }
        }
        System.out.println("去重后的集合：");
        for (Student student : students) {
            System.out.println(student);
        }

        System.out.println("-------------------------------");
        //⑤制定成绩光荣榜（成绩从高到低打印学生成绩）
        for (int i = 0; i < students.size(); i++) {
            for (int j = 0; j < students.size(); j++) {

                if (students.get(i).getScore()>students.get(j).getScore()){
                    double temp = 0;
                    temp = students.get(i).getScore();
                    students.get(i).setScore(students.get(j).getScore());
                    students.get(j).setScore(temp);
                }
            }
        }
        for (Student student : students) {
            System.out.println(student);
        }
    }
}
